package com.jqweb.services;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jqweb.controller.User.pojo.LoginDto;
import com.jqweb.controller.User.pojo.RegisterDto;
import com.jqweb.controller.User.vo.LoginVo;
import com.jqweb.dao.AddressDao;
import com.jqweb.dao.AdminDao;
import com.jqweb.dao.UserDao;
import com.jqweb.entity.AddressEntity;
import com.jqweb.entity.AdminEntity;
import com.jqweb.entity.UserEntity;
import com.jqweb.utils.JWT;
import com.jqweb.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private AdminDao adminDao;

    @Autowired
    private AddressDao addressDao;

    // 登录逻辑
    public Map<String, Object> Login(LoginDto loginDto) {
        // 创建查询条件
        QueryWrapper<UserEntity> result = new QueryWrapper<>();
        // eq 查询 相同匹配
        result.eq("password", MD5.md5(loginDto.getPassword()));
        result.eq("phone", loginDto.getPhone());
        // 根据条件查询用户信息
        UserEntity user = userDao.selectOne(result);
        if (user == null) {
            return null;
        }
        // 封装用户信息
        HashMap<String, Object> userInfo = new HashMap<String, Object>();
        userInfo.put("user_name", user.getUser_name());
        userInfo.put("id", user.getId().toString());
        userInfo.put("phone", user.getPhone());
        userInfo.put("created_time", user.getCreated_time().toString());
        // 生成token
        String token = JWT.setJwt(userInfo);
        // 封装返回的数据
        HashMap<String, Object> data = new HashMap<String, Object>();
        data.put("userInfo", userInfo);
        data.put("token", token);
        return data;
    }

    // 注册逻辑
    public Map<String, Object> Register(RegisterDto registerDto) {
        // 创建查询条件
        QueryWrapper<UserEntity> result = new QueryWrapper<>();
        result.eq("phone", registerDto.getPhone());
        // 根据条件查询用户信息
        UserEntity user = userDao.selectOne(result);
        // 判断用户是否存在
        if (user != null) {
            return null;
        }
        // 创建用户
        UserEntity userEntity = new UserEntity();
        userEntity.setUser_name(registerDto.getUser_name());
        userEntity.setPassword(MD5.md5(registerDto.getPassword()));
        userEntity.setPhone(registerDto.getPhone());
        // 插入用户
        userDao.insert(userEntity);
        // 查询用户
        UserEntity newUser = userDao.selectOne(result);
        // 判断用户是否存在 不存在则说明注册失败
        if (newUser == null) {
            return null;
        }
        // 封装用户信息
        HashMap<String, Object> userInfo = new HashMap<String, Object>();
        userInfo.put("user_name", newUser.getUser_name());
        userInfo.put("id", newUser.getId().toString());
        userInfo.put("phone", newUser.getPhone());
        userInfo.put("created_time", newUser.getCreated_time().toString());
        // 生成token
        String token = JWT.setJwt(userInfo);
        // 封装返回的数据
        HashMap<String, Object> data = new HashMap<String, Object>();
        data.put("userInfo", userInfo);
        data.put("token", token);
        return data;
    }

    // 管理员登陆
    public Map<String, Object> managerLogin(LoginDto loginDto) {
        // 创建查询条件
        QueryWrapper<AdminEntity> result = new QueryWrapper<>();
        // eq 查询 相同匹配
        result.eq("password",  MD5.md5(loginDto.getPassword()));
        result.eq("phone", loginDto.getPhone());
        // 根据条件查询用户信息
        AdminEntity user = adminDao.selectOne(result);
        if (user == null) {
            return null;
        }
        // 封装用户信息
        HashMap<String, Object> userInfo = new HashMap<String, Object>();
        userInfo.put("user_name", user.getUser_name());
        userInfo.put("id", user.getId().toString());
        userInfo.put("phone", user.getPhone());
        userInfo.put("created_time", user.getCreated_time().toString());
        // 生成token
        String token = JWT.setJwt(userInfo);
        // 封装返回的数据
        HashMap<String, Object> data = new HashMap<String, Object>();
        data.put("userInfo", userInfo);
        data.put("token", token);
        return data;
    }

    // 管理员新增用户
    public Integer addUser(RegisterDto register) {
        UserEntity userEntity = new UserEntity();
        userEntity.setUser_name(register.getUser_name());
        userEntity.setPassword(MD5.md5("123123"));
        userEntity.setPhone(register.getPhone());
        return userDao.insert(userEntity);
    }

    // 删除用户
    public Integer deleteUser(Integer id) {
        return userDao.deleteById(id);
    }

    // 获取用户地址信息列表
    public List<AddressEntity> getUserAddress(Integer id) {
        QueryWrapper<AddressEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        return addressDao.selectList(queryWrapper);
    }

    // 新增地址
    public Integer addAddress(AddressEntity address) {
        // 构建地址实体
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setUser_id(address.getUser_id());
        addressEntity.setHarvest(address.getHarvest());
        addressEntity.setHarvest_address(address.getHarvest_address());
        // 插入地址
        return addressDao.insert(addressEntity);
    }

    // 获取所有用户信息
    public List<UserEntity> getAllUser() {
        return userDao.selectList(null);
    }

    // 删除地址
    public Integer deleteAddress(Integer id) {
        return addressDao.deleteById(id);
    }

    // 更新地址
    public Integer updateAddress(AddressEntity address) {
        // 创建查询条件
        QueryWrapper<AddressEntity> queryWrapper = new QueryWrapper<>();
        // 判断地址是否存在
        queryWrapper.eq("id", address.getId());
        if (addressDao.selectOne(queryWrapper) == null) {
            return null;
        }
        // 构建地址实体
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setId(address.getId());
        addressEntity.setUser_id(address.getUser_id());
        addressEntity.setHarvest(address.getHarvest());
        addressEntity.setHarvest_address(address.getHarvest_address());
        // 根据id 更新地址
        return addressDao.updateById(addressEntity);
    }

    // 修改用户信息
    public Integer updateUser(UserEntity userEntity) {
        // 根据id查询用户信息
        UserEntity user =  userDao.selectById(userEntity.getId());
        // 如果用户不存在就返回null
        if (user == null){
            return null;
        }
        // 构建新的用户信息
        UserEntity userItem= new UserEntity();
        userItem.setId(userEntity.getId());
        userItem.setUser_name(userEntity.getUser_name());
        userItem.setPhone(userEntity.getPhone());
        // 更新用户信息
        return userDao.updateById(userEntity);
    }

    // 根据token信息获取用户信息
    public UserEntity getUserInfo(String id) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        // 获取用户信息
        return userDao.selectOne(queryWrapper);
    }
}
